TweetUser.isCreatedRecently   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1 5
import moment from 'moment';
2
import {Twitter} from 'twit';
3 5
import Constant from '../Constant';
4 5
import Helper from '../Helper';
5 5
import {ReTweetableAbstract, Validation} from '../ReTweetable';
6
7
export type TweetUserConfig = {
8
    minCreationDiff: number,
9
    minFollowers: number,
10
    minTweets: number,
11
    userBlocklist?: string[]
12
};
13
14 5
export default class TweetUser extends ReTweetableAbstract {
15
    rawUser: Twitter.User;
16
    config: TweetUserConfig;
17
18
    constructor(rawUser: Twitter.User, config: TweetUserConfig) {
19 58
        super();
20 58
        this.rawUser = rawUser;
21 58
        this.config = config;
22
    }
23
24
    isPublic(): boolean {
25 12
        return !this.rawUser.protected;
26
    }
27
28
    isCreatedRecently(): boolean {
29 10
        const createdAt = moment(this.rawUser.created_at, Constant.TWITTER_DATETIME_FORMAT, Constant.LANG_EN);
30 10
        const now = moment();
31
32 10
        return now.diff(createdAt, Constant.MOMENT_DAYS) < this.config.minCreationDiff;
33
    }
34
35
    hasEnoughFollowers(): boolean {
36 9
        return this.rawUser.followers_count >= this.config.minFollowers;
37
    }
38
39
    hasEnoughTweets(): boolean {
40 8
        return this.rawUser.statuses_count >= this.config.minTweets;
41
    }
42
43
    isBlockListed(): boolean {
44 13
        return Helper.objectExists(this.config.userBlocklist) && this.config.userBlocklist.includes(this.rawUser.id_str);
45
    }
46
47
    getRetweetValidations(): Validation[] {
48 11
        return [
49
            {
50 11
                validate: () => this.isBlockListed(),
51 1
                message: () => 'User is in blocklist'
52
            },
53
            {
54 10
                validate: () => !this.isPublic(),
55 2
                message: () => 'User is not public'
56
            },
57
            {
58 8
                validate: () => this.isCreatedRecently(),
59 1
                message: () => 'User is created recently'
60
            },
61
            {
62 7
                validate: () => !this.hasEnoughFollowers(),
63 1
                message: () => 'User does not have enough followers'
64
            },
65
            {
66 6
                validate: () => !this.hasEnoughTweets(),
67 1
                message: () => 'User does not have enough tweets'
68
            }
69
        ];
70
    }
71
}